#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>

SDL_AudioSpec * AudioSpecDesired;
SDL_AudioSpec * AudioSpecObtained;
void AudioCallback(void* userdata,Uint8* buffer,int len)
{
	int index;
	for(index=0;index<len;index++){
		buffer[index]=rand()%256;
	}
}

void PressESC2Quit()
{
	int GameOver=0;
	SDL_Event GameEvent;
	while(GameOver==0){
		//SDL_PollEvent(&GameEvent);
		SDL_WaitEvent(&GameEvent);
		if(GameEvent.type==SDL_QUIT){
			GameOver=1;
		}else if(GameEvent.type==SDL_KEYDOWN){
			if(GameEvent.key.keysym.sym==SDLK_ESCAPE){
				GameOver=1;
			}
		}
	}
	return;
}

int main(int argc, char *argv[])
{
	if(SDL_Init(SDL_INIT_VIDEO)==-1){
		printf("SDL_Init() fail!\n");
		return 0;
	}
	SDL_Surface *pScreen=NULL;
	SDL_Surface *pBmp=NULL;
	
	AudioSpecObtained=malloc(sizeof(SDL_AudioSpec));
	AudioSpecDesired=malloc(sizeof(SDL_AudioSpec));
	AudioSpecDesired->freq=22050;
	AudioSpecDesired->format=AUDIO_S16LSB;
	AudioSpecDesired->channels=1;
	AudioSpecDesired->samples=8192;
	AudioSpecDesired->callback=AudioCallback;
	AudioSpecDesired->userdata=NULL;
	
	SDL_OpenAudio(AudioSpecDesired,AudioSpecObtained);
	SDL_PauseAudio(0);
	
	pScreen=SDL_SetVideoMode(720,450,32,SDL_SWSURFACE);

	pBmp=SDL_LoadBMP("sun.bmp");
	
	if(pBmp==NULL){
		printf("SDL_LoadBMP() load %s fail!\n",argv[1]);
		return 0;
	}
	SDL_BlitSurface(pBmp,NULL,pScreen,NULL);
	SDL_Flip(pScreen);
	
	PressESC2Quit();
	SDL_CloseAudio();
	SDL_Quit();
	free(AudioSpecObtained);
	free(AudioSpecDesired);
	return 0;
}